package com.yunfeisoft.business.controller;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.service.inter.UserService;
import com.yunfeisoft.business.model.ReceiveMealApply;
import com.yunfeisoft.business.service.inter.ReceiveMealApplyService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: ReceiveMealApplyController
 * Description: 接待用餐申请单Controller
 * Author: Jackie liu
 * Date: 2020-01-09
 */
@RestController
public class ReceiveMealApplyController extends BaseController {

    @Autowired
    private ReceiveMealApplyService receiveMealApplyService;
    @Autowired
    private UserService userService;

    /**
     * 添加接待用餐申请单
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/receiveMealApply/save", method = RequestMethod.POST)
    public Response save(ReceiveMealApply record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "workflowDefineId", "流程定义参数错误");
        validator.required(request, "nextStepUserId", "处理人为空");
        validator.required(request, "receiveTime", "日期为空");
        validator.required(request, "visitUnit", "来访单位为空");
        validator.required(request, "mealNum", "用餐人数为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        receiveMealApplyService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改接待用餐申请单
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/receiveMealApply/modify", method = RequestMethod.POST)
    public Response modify(ReceiveMealApply record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "workflowDefineId", "流程定义参数错误");
        validator.required(request, "receiveTime", "日期为空");
        validator.required(request, "visitUnit", "来访单位为空");
        validator.required(request, "mealNum", "用餐人数为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        receiveMealApplyService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询接待用餐申请单
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/receiveMealApply/query", method = {RequestMethod.POST, RequestMethod.GET})
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        ReceiveMealApply record = receiveMealApplyService.load(id);
        if (record != null) {
            User user = userService.load(record.getCreateId());
            record.setCreateName(user.getName());
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询接待用餐申请单
     *
     * @param request
     * @param response
     * @return
     */
    /*@RequestMapping(value = "/web/receiveMealApply/list", method = {RequestMethod.POST, RequestMethod.GET})
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        Page<ReceiveMealApply> page = receiveMealApplyService.queryPage(params);
        return ResponseUtils.success(page);
    }*/

    /**
     * 批量删除接待用餐申请单
     *
     * @param request
     * @param response
     * @return
     */
    /*@RequestMapping(value = "/web/receiveMealApply/delete", method = {RequestMethod.POST, RequestMethod.GET})
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        receiveMealApplyService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }*/
}
